package com.sqfw.project.wp.service.impl;

import java.util.Date;
import java.util.List;

import com.sqfw.common.utils.DateUtils;
import com.sqfw.common.utils.SecurityUtils;
import com.sqfw.common.utils.StringUtils;
import com.sqfw.project.system.domain.SysDept;
import com.sqfw.project.system.domain.SysUser;
import com.sqfw.project.system.mapper.SysDeptMapper;
import com.sqfw.project.wp.constant.ApplyState;
import com.sqfw.project.wp.domain.WpReviewedItemLog;
import com.sqfw.project.wp.mapper.WpReviewedItemLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sqfw.project.wp.mapper.WpApplyItemMapper;
import com.sqfw.project.wp.domain.WpApplyItem;
import com.sqfw.project.wp.service.IWpApplyItemService;

/**
 * 工单申请管理Service业务层处理
 *
 * @author lx
 * @date 2023-08-14
 */
@Service
public class WpApplyItemServiceImpl implements IWpApplyItemService {
    @Autowired
    private WpApplyItemMapper wpApplyItemMapper;

    @Autowired
    private WpReviewedItemLogMapper wpReviewedItemLogMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    /**
     * 查询工单申请管理
     *
     * @param autoId 工单申请管理主键
     * @return 工单申请管理
     */
    @Override
    public WpApplyItem selectWpApplyItemByAutoId(Long autoId) {
        return wpApplyItemMapper.selectWpApplyItemByAutoId(autoId);
    }

    /**
     * 查询工单申请管理列表
     *
     * @param wpApplyItem 工单申请管理
     * @return 工单申请管理
     */
    @Override
    public List<WpApplyItem> selectWpApplyItemList(WpApplyItem wpApplyItem) {
        if (ApplyState.LIANLUOYUAN.equals(wpApplyItem.getApplyState())||ApplyState.CBDWLINGDAO.equals(wpApplyItem.getApplyState())){
            wpApplyItem.setApplyDeptId(SecurityUtils.getDeptId());
            wpApplyItem.setApplyState(null);
        }

        List<WpApplyItem> wpApplyItems = wpApplyItemMapper.selectWpApplyItemList(wpApplyItem);

        for (WpApplyItem applyItem : wpApplyItems) {
            applyItem.setStage(ApplyState.wpState(applyItem.getApplyState(),applyItem.getStatus()));
            SysDept sysDept = deptMapper.selectDeptById(applyItem.getDeptId());
            applyItem.setApplyDeptName(sysDept.getDeptName());
            applyItem.setDownLoadStatus(false);
            if ("1".equals(applyItem.getStatus())){
                applyItem.setDownLoadStatus(true);
                if ("临时性".equals(applyItem.getApplyType()) && applyItem.getDownloadCount() >= 1){
                    applyItem.setDownLoadStatus(false);
                }
            }

        }
        return wpApplyItems;
    }

    /**
     * 新增工单申请管理
     *
     * @param wpApplyItem 工单申请管理
     * @return 结果
     */
    @Override
    public int insertWpApplyItem(WpApplyItem wpApplyItem) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Date date = DateUtils.getNowDate();
        wpApplyItem.setApplyDeptId(user.getDeptId());
        wpApplyItem.setDeptId(user.getDeptId());
        wpApplyItem.setCreateTime(date);
        wpApplyItem.setApplyTime(date);
//        wpApplyItem.setStatus("2");
        //20250610洪磊  默认审核通过
        wpApplyItem.setStatus("1");
        //申请人
        wpApplyItem.setApplyUser(user.getCode());
        //创建人
        wpApplyItem.setCreateBy(user.getNickName());

        return wpApplyItemMapper.insertWpApplyItem(wpApplyItem);
    }

    /**
     * 修改工单申请管理
     *
     * @param wpApplyItem 工单申请管理
     * @return 结果
     */
    @Override
    public int updateWpApplyItem(WpApplyItem wpApplyItem) {
        SysUser user = SecurityUtils.getLoginUser().getUser();

        if (StringUtils.isEmpty(wpApplyItem.getApplyState())){
            wpApplyItem.setApplyState(null);
        }
        wpApplyItem.setUpdateTime(DateUtils.getNowDate());
        wpApplyItem.setUpdateBy(user.getUserName());
        //20250611 洪磊注释 新增和修改均为审核通过的
//        if ("0".equals(wpApplyItem.getReviewedSatus())){
//            //审核不通过
//            wpApplyItem.setStatus("0");
//        }
//        if ("1".equals(wpApplyItem.getReviewedSatus())&&"5".equals(wpApplyItem.getApplyState())){
//            wpApplyItem.setStatus("1");
//        }
        //审核状态
        this.insertWpReviewedItemLog(wpApplyItem);
        return wpApplyItemMapper.updateWpApplyItem(wpApplyItem);
    }

    @Override
    public List<WpApplyItem> selectWpApplyItemListByStatus(WpApplyItem item) {
        Long deptId = SecurityUtils.getDeptId();
        item.setDeptId(deptId);
        item.setStatus("2");
        return wpApplyItemMapper.selectWpApplyItemList(item);
    }

    /**
     * 申请审核记录
     *
     * @param wpApplyItem
     */
    public void insertWpReviewedItemLog(WpApplyItem wpApplyItem) {

        WpApplyItem applyItem = wpApplyItemMapper.selectWpApplyItemByAutoId(wpApplyItem.getAutoId());

        SysUser user = SecurityUtils.getLoginUser().getUser();

        WpReviewedItemLog wpReviewedItemLog = new WpReviewedItemLog();

        wpReviewedItemLog.setApplyId(wpApplyItem.getAutoId());
        wpReviewedItemLog.setReviewedUser(user.getCode());
        wpReviewedItemLog.setCreateBy(user.getUserName());
        //创建时间，及审核时间
        wpReviewedItemLog.setCreateTime(new Date());
        //上个状态
        wpReviewedItemLog.setLastState(applyItem.getApplyState());
        wpReviewedItemLog.setState(wpApplyItem.getApplyState());

        wpReviewedItemLog.setReviewedSatus(wpApplyItem.getReviewedSatus());
        wpReviewedItemLogMapper.insertWpReviewedItemLog(wpReviewedItemLog);

    }
}
